<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="utf-8">
    <title>发布统计</title>
    <link rel="stylesheet" href="/app/admin/component/pear/css/pear.css"/>
    <link rel="stylesheet" href="/app/admin/admin/css/reset.css"/>
 
</head>
<body class="pear-container">

<!-- 筛选表单 -->
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" lay-filter="statistics-form">

            <!-- 时间筛选区域 -->
            <div class="layui-form-item">
                <label class="layui-form-label">时间范围</label>
                <div class="layui-input-block">
                    <div class="layui-col-md4">
                        <input type="text" name="start_date" id="start_date" placeholder="开始日期" class="layui-input">
                    </div>
                    <div class="layui-col-md1" style="text-align: center; line-height: 38px;">至</div>
                    <div class="layui-col-md4">
                        <input type="text" name="end_date" id="end_date" placeholder="结束日期" class="layui-input">
                    </div>
                    <div class="layui-col-md3">
                        <div name="quick_date" id="quick_date" lay-filter="quick_date"></div>
                    </div>
                </div>
            </div>

            <!-- 站点筛选区域 -->
            <div class="layui-form-item">
                <label class="layui-form-label">源站筛选</label>
                <div class="layui-input-block">
                    <div class="layui-col-md6">
                        <div name="source_site_id" id="source_site_id" lay-filter="source_site_id"></div>
                    </div>
                </div>
            </div>

            <!-- 操作按钮 -->
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="statistics-query">
                        <i class="layui-icon layui-icon-search"></i>查询统计
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md" lay-submit lay-filter="statistics-reset">
                        <i class="layui-icon layui-icon-refresh"></i>重置
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>

<!-- 统计表格 -->
<div class="layui-card">
    <div class="layui-card-body">
        <table class="layui-hide" id="statistics-table" lay-filter="statistics-table"></table>
    </div>
</div>

<script src="/app/admin/component/layui/layui.js?v=2.8.12"></script>
<script src="/app/admin/component/pear/pear.js"></script>
<script src="/app/admin/admin/js/permission.js"></script>
<script src="/app/admin/admin/js/common.js"></script>
<script>
layui.use(["table", "form", "laydate", "xmSelect", "common", "popup", "util"], function () {
    let table = layui.table;
    let form = layui.form;
    let laydate = layui.laydate;
    let $ = layui.$;
    let common = layui.common;
    let popup = layui.popup;
    let util = layui.util;


    // 日期选择器
    laydate.render({
        elem: "#start_date",
        type: "date"
    });

    laydate.render({
        elem: "#end_date",
        type: "date"
    });

    // 快速日期选择下拉框
    layui.xmSelect.render({
        el: "#quick_date",
        name: "quick_date",
        data: [
            {name: "今天", value: "today"},
            {name: "昨天", value: "yesterday"},
            {name: "本周", value: "this_week"},
            {name: "上周", value: "last_week"},
            {name: "本月", value: "this_month"},
            {name: "上月", value: "last_month"},
            {name: "今年", value: "this_year"},
            {name: "去年", value: "last_year"}
        ],
        model: {"icon": "hidden", "label": {"type": "text"}},
        clickClose: true,
        radio: true,
        on: function(data) {
            if (data.arr.length > 0) {
                let quickDate = data.arr[0].value;
                setQuickDateRange(quickDate);
            }
        }
    });

    // 源站选择下拉框
    layui.xmSelect.render({
        el: "#source_site_id",
        name: "source_site_id",
        data: [],
        model: {"icon": "hidden", "label": {"type": "text"}},
        clickClose: true,
        radio: true,
        placeholder: "选择源站"
    });

    // 加载站点数据
    loadSiteData();

    // 设置快速日期范围
    function setQuickDateRange(quickDate) {
        let startDate = "";
        let endDate = "";
        let now = new Date();

        switch (quickDate) {
            case "today":
                startDate = endDate = util.toDateString(now, "yyyy-MM-dd");
                break;
            case "yesterday":
                let yesterday = new Date(now.getTime() - 24 * 60 * 60 * 1000);
                startDate = endDate = util.toDateString(yesterday, "yyyy-MM-dd");
                break;
            case "this_week":
                let weekStart = new Date(now.getTime() - now.getDay() * 24 * 60 * 60 * 1000);
                startDate = util.toDateString(weekStart, "yyyy-MM-dd");
                endDate = util.toDateString(now, "yyyy-MM-dd");
                break;
            case "last_week":
                let lastWeekEnd = new Date(now.getTime() - now.getDay() * 24 * 60 * 60 * 1000);
                let lastWeekStart = new Date(lastWeekEnd.getTime() - 7 * 24 * 60 * 60 * 1000);
                startDate = util.toDateString(lastWeekStart, "yyyy-MM-dd");
                endDate = util.toDateString(lastWeekEnd, "yyyy-MM-dd");
                break;
            case "this_month":
                let monthStart = new Date(now.getFullYear(), now.getMonth(), 1);
                startDate = util.toDateString(monthStart, "yyyy-MM-dd");
                endDate = util.toDateString(now, "yyyy-MM-dd");
                break;
            case "last_month":
                let lastMonthStart = new Date(now.getFullYear(), now.getMonth() - 1, 1);
                let lastMonthEnd = new Date(now.getFullYear(), now.getMonth(), 0);
                startDate = util.toDateString(lastMonthStart, "yyyy-MM-dd");
                endDate = util.toDateString(lastMonthEnd, "yyyy-MM-dd");
                break;
            case "this_year":
                startDate = now.getFullYear() + "-01-01";
                endDate = util.toDateString(now, "yyyy-MM-dd");
                break;
            case "last_year":
                startDate = (now.getFullYear() - 1) + "-01-01";
                endDate = (now.getFullYear() - 1) + "-12-31";
                break;
        }

        $("#start_date").val(startDate);
        $("#end_date").val(endDate);
    }

    // 渲染统计表格
    function renderStatisticsTable(startDate, endDate, quickDate, sourceSiteId) {
        let whereData = {};
        if (startDate) whereData.start_date = startDate;
        if (endDate) whereData.end_date = endDate;
        if (quickDate) whereData.quick_date = quickDate;
        if (sourceSiteId) whereData.source_site_id = sourceSiteId;

        table.render({
            elem: "#statistics-table",
            url: "/app/csauto/api/torrentreleasestatistics/getStatistics",
            method: "post",
            contentType: "application/x-www-form-urlencoded",
            where: whereData,
            page: false,
            cols: [[
                {
                    title: "站点名称",
                    field: "site_name",
                    width: 150,
                    align: "center"
                },
                {
                    title: "待发布",
                    field: "task_status_0",
                    width: 100,
                    align: "center",
                    templet: function(d) {
                        return '<span style="color: #FFB800;">' + d.task_status_0 + '</span>';
                    }
                },
                {
                    title: "发布成功",
                    field: "task_status_1",
                    width: 100,
                    align: "center",
                    templet: function(d) {
                        return '<span style="color: #5FB878;">' + d.task_status_1 + '</span>';
                    }
                },
                {
                    title: "资源重复",
                    field: "task_status_2",
                    width: 100,
                    align: "center",
                    templet: function(d) {
                        return '<span style="color: #FF5722;">' + d.task_status_2 + '</span>';
                    }
                },
                {
                    title: "发布失败",
                    field: "task_status_3",
                    width: 100,
                    align: "center",
                    templet: function(d) {
                        return '<span style="color: #FF5722;">' + d.task_status_3 + '</span>';
                    }
                },
                {
                    title: "任务不存在",
                    field: "task_status_4",
                    width: 100,
                    align: "center",
                    templet: function(d) {
                        return '<span style="color: #FF5722;">' + d.task_status_4 + '</span>';
                    }
                },
                {
                    title: "任务已结束",
                    field: "task_status_5",
                    width: 100,
                    align: "center",
                    templet: function(d) {
                        return '<span style="color: #FF5722;">' + d.task_status_5 + '</span>';
                    }
                },
                {
                    title: "源站种子不存在",
                    field: "task_status_6",
                    width: 120,
                    align: "center",
                    templet: function(d) {
                        return '<span style="color: #FF5722;">' + d.task_status_6 + '</span>';
                    }
                },
                {
                    title: "完结",
                    field: "task_status_99",
                    width: 100,
                    align: "center",
                    templet: function(d) {
                        return '<span style="color: #999;">' + d.task_status_99 + '</span>';
                    }
                },
                {
                    title: "总计",
                    field: "total",
                    width: 100,
                    align: "center",
                    templet: function(d) {
                        return '<strong>' + d.total + '</strong>';
                    }
                }
            ]],
            done: function(res) {
                if (res.code !== 0) {
                    popup.failure(res.msg || "获取统计数据失败");
                }
            }
        });
    }


    // 查询统计数据
    form.on("submit(statistics-query)", function(data) {
        let startDate = $("#start_date").val();
        let endDate = $("#end_date").val();

        // 获取xmSelect的值
        let xmSelect = layui.xmSelect.get('#quick_date', true);
        let quickDate = xmSelect ? xmSelect.getValue('value')[0] : null;

        // 获取源站筛选值
        let sourceXmSelect = layui.xmSelect.get('#source_site_id', true);
        let sourceSiteId = sourceXmSelect ? sourceXmSelect.getValue('value')[0] : null;

        renderStatisticsTable(startDate, endDate, quickDate, sourceSiteId);
        return false;
    });

    // 重置表单
    form.on("submit(statistics-reset)", function(data) {
        $("#statistics-form")[0].reset();
        form.render();

        // 重置所有xmSelect
        let xmSelectQuick = layui.xmSelect.get('#quick_date', true);
        if (xmSelectQuick) {
            xmSelectQuick.setValue([]);
        }

        let xmSelectSource = layui.xmSelect.get('#source_site_id', true);
        if (xmSelectSource) {
            xmSelectSource.setValue([]);
        }

        renderStatisticsTable(); // 重新渲染表格，使用默认时间范围
        return false;
    });

    // 加载站点数据
    function loadSiteData() {
        layui.$.ajax({
            url: "/app/csauto/api/get?name=site",
            dataType: "json",
            success: function (res) {
                if (res.code === 0) {
                    // 等待一小段时间确保xmSelect组件已完全初始化
                    setTimeout(function() {
                        // 更新源站选择器
                        let sourceXmSelect = layui.xmSelect.get('#source_site_id', true);
                        if (sourceXmSelect) {
                            sourceXmSelect.update({
                                data: res.data
                            });
                        }
                    }, 50);
                }
            },
            error: function() {
                console.error("加载站点数据失败");
            }
        });
    }

    // 初始加载（使用默认时间范围）
    renderStatisticsTable(null, null, null, null);
});

</script>
</body>
</html>
